-- http://www.hammerspoon.org/
-- initlua 可以这么导入该脚本 dofile("/Users/ale/Desktop/gitee/my-cmd/lua/hammerspoon/init.lua")

-- 方法必须先声明后使用
local function isTargetApp(appName)
  return hs.application.frontmostApplication():name() == appName
end

----- 2. 热键 之 重写快捷键 -----
-- 目前只能先绑定快捷键，再判断应用，就这样吧

-- hs.hotkey.bind({"cmd"}, "q", function()
--   -- 在这里添加您想要执行的操作
--   if ( isTargetApp('访达') ) then hs.eventtap.keyStroke({"cmd"}, "w")
--   elseif ( isTargetApp('QQ音乐123') ) then
--   end
-- end)

-- hs.hotkey.bind({"cmd"}, "[", function()
--   -- 在这里添加您想要执行的操作
--   if ( isTargetApp('Code') ) then hs.alert.show("Hello World!")
--   elseif ( isTargetApp('QQ音乐') ) then hs.eventtap.keyStroke({"cmd"}, "left")
--   else -- 不捕获按键应该怎么写
--     -- 不捕获按键
--     hs.eventtap.cancel() 
--   end
-- end)

-- hs.hotkey.bind({"cmd"}, "[", function()
--   -- 在这里添加您想要执行的操作
--   if ( isTargetApp('Code') ) then hs.alert.show("Hello World!")
--   elseif ( isTargetApp('QQ音乐') ) then hs.eventtap.keyStroke({"cmd"}, "left")
--   end
-- end)

-- hs.hotkey.bind({"ctrl", "alt"}, "v", function()
--   hs.eventtap.keyStroke({"cmd"}, "v")
-- end)

----- 3. 热键 之 打开网址 -----
local url_bilibili = "https://www.bilibili.com/"
hs.hotkey.bind({"alt"}, "6", function() hs.urlevent.openURL(url_bilibili) end)
hs.hotkey.bind({"alt"}, "7", function() hs.urlevent.openURL("https://www.ithome.com") end)
hs.hotkey.bind({"alt"}, "8", function() hs.urlevent.openURL("https://www.ixigua.com") end)

----- 4. 热键 之 运行程序 -----
hs.hotkey.bind({"alt"}, "1", function()
  if (isTargetApp('访达'))
  then
    hs.application.frontmostApplication():hide()
  else
    hs.application.launchOrFocus("Finder")
  end
end)
hs.hotkey.bind({"alt"}, "2", function()
  if (isTargetApp('360极速浏览器Pro'))
  then
    hs.application.frontmostApplication():hide()
  else
    hs.application.launchOrFocus("360Chrome")
  end
end)
hs.hotkey.bind({"alt"}, "4", function() hs.application.launchOrFocus("iTerm") end)
hs.hotkey.bind({"alt"}, "5", function()
  if (isTargetApp('Code'))
  then
    hs.application.frontmostApplication():hide()
  else
    hs.application.launchOrFocus("Visual Studio Code")
  end
end)
-- open 必须是否匹配全路径，目前看是没有必要直接使用 open 了。 hs.application.open("/System/Applications/Weather.app")

----- 5. 热键 之 启动文件夹 -----

----- 6. 热键 之 其他 -----

-- 通过发出假键盘事件来键入剪贴板的内容来绕过校验
hs.hotkey.bind({"cmd", "alt"}, "V", function() hs.eventtap.keyStrokes(hs.pasteboard.getContents()) end)

-- 使用 spoon 的实例
hs.loadSpoon("AClock")
spoon.AClock:init()
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "C", function()
  spoon.AClock:toggleShow()
end)

-- 自动重启脚本
function reloadConfig(files)
  doReload = false
  for _,file in pairs(files) do
      if file:sub(-4) == ".lua" then
          doReload = true
      end
  end
  if doReload then
      hs.reload()
  end
end
myWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded")

-- 辅助方法，用于查看前台应用
hs.hotkey.bind({'ctrl', 'alt'}, ".",
  function()
    hs.alert.show(string.format("App path:  %s\nApp name: %s\nIM source id:  %s",
        hs.window.focusedWindow():application():path(),
        hs.window.focusedWindow():application():name(),
        hs.keycodes.currentSourceID()))
end)

-- alt + ctrl + 方向键用于调整窗口位置
hs.hotkey.bind({"alt", "ctrl"}, "Left", function()
  local win = hs.window.focusedWindow()
  local f = win:frame()
  local screen = win:screen()
  local max = screen:frame()

  f.x = max.x
  f.y = max.y
  f.w = max.w / 2
  f.h = max.h
  win:setFrame(f)
end)

hs.hotkey.bind({"alt", "ctrl"}, "Right", function()
  local win = hs.window.focusedWindow()
  local f = win:frame()
  local screen = win:screen()
  local max = screen:frame()

  f.x = max.x + (max.w / 2)
  f.y = max.y
  f.w = max.w / 2
  f.h = max.h
  win:setFrame(f)
end)

hs.hotkey.bind({"alt", "ctrl"}, "Up", function()
  local win = hs.window.focusedWindow()
  local screen = win:screen()
  local frame = screen:frame()
  
  local f = win:frame()
  f.x = frame.x
  f.y = frame.y
  f.w = frame.w
  f.h = frame.h

  -- hs.alert.show(f.x)
  -- hs.alert.show(f.y)
  -- hs.alert.show(frame.w)
  -- hs.alert.show(frame.h)

  win:setFrame(f)
end)

hs.hotkey.bind({"alt", "ctrl"}, "Down", function()
  local win = hs.window.focusedWindow()
  local screen = win:screen()
  local max = screen:frame()
  
  local f = win:frame()
  f.x = max.x
  f.y = max.y + (max.h / 2)
  f.w = max.w
  f.h = max.h / 2

  win:setFrame(f)
end)

hs.hotkey.bind({"alt", "ctrl"}, "d", function()
  local zhangsan = hs.application.frontmostApplication():allWindows()
  -- 获取数组的长度 
  hs.alert.show(#zhangsan)
  -- 获取当前这个的下标
  for k, v in pairs(zhangsan) do
    if v == hs.window.frontmostWindow() then
      -- v:moveOneScreenEast()
    end
    -- hs.alert.show(v:isFrontmost())
    -- v:
  end
  -- hs.alert.show(zhangsan[1])
  -- 激活下一个元素
  -- zhangsan[1]:minimize()
end)
